package com.jinshan.gaietymall.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinshan.gaietymall.core.exception.BusinessException;
import com.jinshan.gaietymall.entity.TRole;
import com.jinshan.gaietymall.entity.request.role.TRoleIdReqVO;
import com.jinshan.gaietymall.entity.request.role.TRoleInsertReqVO;
import com.jinshan.gaietymall.entity.request.role.TRoleQueryVO;
import com.jinshan.gaietymall.entity.response.role.TRoleRep;
import com.jinshan.gaietymall.exception.ErrorCodesEnum;
import com.jinshan.gaietymall.service.ITRoleService;
import com.jinshan.gaietymall.utils.BasePageResponseResult;
import com.jinshan.gaietymall.utils.BaseResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author liujinshan
 * @since 2020-04-15
 */
@RestController
@RequestMapping("/admin/role")
@CrossOrigin
public class TRoleController {


    @Autowired
    private ITRoleService itRoleService;

    /**
     * @description  角色列表
     * @author liujinshan
     * @date 2020/4/24 13:01
     */
    @PostMapping(value = "/getPageRoleInfo", produces = {"application/json"}, consumes = {"application/json"})
    public BasePageResponseResult<TRoleRep> getPage(@RequestBody TRoleQueryVO tRoleQueryVO){
        BasePageResponseResult<TRoleRep> basePageResponseResult = new BasePageResponseResult<>();
        try {
            IPage<TRoleRep> userPage = itRoleService.getPage(tRoleQueryVO);
            basePageResponseResult = BasePageResponseResult.ok(userPage);
        } catch (BusinessException e) {
            basePageResponseResult.setErrMsg(e.getMsg());
            basePageResponseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            basePageResponseResult.setErrMsg(e.toString());
            basePageResponseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return basePageResponseResult;
    }

    /**
     * @description 根据ID查询角色信息
     * @author liujinshan
     * param null
     * @return
     * @throws
     * @date 2020/4/24 13:02
     */
    @PostMapping(value = "/getById", produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TRole> getById(@RequestBody  TRoleIdReqVO tRoleIdReqVO){
        BaseResponseResult<TRole> responseResult = new BaseResponseResult<>();
        try {
            responseResult = BaseResponseResult.ok(itRoleService.getById(tRoleIdReqVO));
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }

    /**
     * @description 角色更新
     * @author liujinshan
     * param null
     * @return
     * @throws
     * @date 2020/4/24 15:15
     */
    @PostMapping(value = "update", produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TRole> update(){
        BaseResponseResult<TRole> responseResult = new BaseResponseResult<>();
        try {
            responseResult = BaseResponseResult.ok(itRoleService.update());
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }


    /**
     * @description 角色删除
     * @author liujinshan
     * param null
     * @return
     * @throws
     * @date 2020/4/24 15:15
     */
    @PostMapping(value = "delete", produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TRole> delete(){
        BaseResponseResult<TRole> responseResult = new BaseResponseResult<>();
        try {
            responseResult = BaseResponseResult.ok(itRoleService.removeById(1));
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }


    /**
     * @description 角色新增
     * @author liujinshan
     * param tUser
     * @return responseResult
     * @date 2020/4/24 15:17
     */
    @PostMapping(value = "add", produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TRole> add(TRoleInsertReqVO  tRoleInsertReqVO){
        BaseResponseResult<TRole> responseResult = new BaseResponseResult<>();
        try {
            itRoleService.add(tRoleInsertReqVO);
            responseResult = BaseResponseResult.ok();
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }

}
